package com.tz.dao;

import com.tz.entity.Product;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/7/27.
 */
@Repository
@Transactional
public class ProductDaoImpl implements ProductDao{

    @Autowired
    private SessionFactory sessionFactory;
    private Session getSession(){
        return  sessionFactory.getCurrentSession();
    }

    @Override
    public List<Product> findProduct() {
        Session session = getSession();
        String hql = "from Product";
        return session.createQuery(hql).list();
    }

    @Override
    public List<Product> findProductBy(String catename) {
        Session session = getSession();
        String hql = "from Product p where p.category.name =?";
        return session.createQuery(hql).setString(0, catename).list();
    }

    @Override
    public Product findProductById(long id) {
        return null;
    }

    @Override
    public void saveProduct(Product p) {
        getSession().save(p);
    }
}
